package com.conseilimmo.repository;

import com.hildeberto.home.util.EntityManagerHelper;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import objetsBd.BatimentBd;
import objetsBd.LocataireBd;
import objetsBd.LocationBd;

/**
 *
 * @author verpoorten
 */
public class LocationBsn {
 private EntityManager em = EntityManagerHelper.EM.open();
    
    public List<LocationBd> trouverListeLocations() {
        String sql = "select b from LocataireBd  b";
        return em.createQuery(sql).getResultList();
    }

    public LocationBd trouverLocationParId(Long idLocation) {
        String sql = "select b from LocationBd  b";
        sql += " where b.idLocation = :idLocation";
        return (LocationBd) em.createQuery(sql).setParameter("idLocation", idLocation).getSingleResult();
    }  
    
    
    public LocationBd trouverLocationParIdBatimentDate(Long idBatiment) {
        String sql = "select b from LocationBd  b";
        sql += " where b.idBatiment = :idBatiment and b.bailDateDebut<= :dateRef and b.bailDateFin >=:dateRef";
        List<LocationBd> locations= em.createQuery(sql).setParameter("idBatiment", idBatiment).setParameter("dateRef",new Date()).getResultList();
        for(LocationBd l :locations){
            return l;
        }
        return null;
    }  
public List<LocationBd> trouverListeLocationsParIdBatiment(Long idBatiment) {
        String sql = "select b from LocationBd  b";
        sql += " where b.idBatiment = :idBatiment";
        return em.createQuery(sql).setParameter("idBatiment", idBatiment).getResultList();
    }    
   public void modifierLocation(LocationBd batimentModifie) {
        em.merge(batimentModifie);
       
    }
    public LocationBd ajouterLocation(LocationBd batimentModifie) {
        return em.merge(batimentModifie);       
    }
}
